var fs = require("fs");
var path = require("path");

var export_files = ["utils", "constant", "point_map", "ad_util", "ocrUtils"];
var generate_files = ["search30", "search2", "shop"];

var all_folder = "头条搜索极速版_all";
var run_folder = "头条搜索极速版_run";
var module_folder = "头条搜索极速版_module";

//run: export_code
var export_code = export_files
  .map(function (file) {
    var code = fs.readFileSync(
      path.join(__dirname, `../${all_folder}/exports/${file}.js`),
      {
        encoding: "utf-8",
      }
    );
    return code.replace(/var[\w\W]+?require[\w\W]+?;/gi, "");
  })
  .join("\n");

generate_files.forEach(function (file) {
  var code = fs.readFileSync(
    path.join(__dirname, `../${all_folder}/${file}.js`),
    {
      encoding: "utf-8",
    }
  );
  var final_code =
    export_code +
    "\n" +
    code.replace(/var[\w\W]+?require[\w\W]+?;/gi, "") +
    "\n" +
    file +
    ".start()";
  fs.writeFileSync(
    path.join(__dirname, `../${run_folder}/${file}.js`),
    final_code,
    {
      encoding: "utf-8",
    }
  );
});

//module: 生成module文件
export_files.forEach(function (file) {
  var code = fs.readFileSync(
    path.join(__dirname, `../${all_folder}/exports/${file}.js`),
    {
      encoding: "utf-8",
    }
  );
  var module_code = code + "\n" + "module.exports=" + file;
  fs.writeFileSync(
    path.join(__dirname, `../${module_folder}/exports/${file}.js`),
    module_code,
    {
      encoding: "utf-8",
    }
  );
});

//module: require_code
var require_code = export_files
  .map(function (export_file) {
    return "var " + export_file + " = require('./exports/" + export_file + "')";
  })
  .join("\n");

//module: 生成文件
generate_files.forEach(function (file) {
  var code = fs.readFileSync(
    path.join(__dirname, `../${all_folder}/${file}.js`),
    {
      encoding: "utf-8",
    }
  );
  var final_code =
    require_code + "\n" + code + "\n" + "module.exports = " + file;
  var run_code = require_code + "\n" + code + "\n" + file + ".start()";
  fs.writeFileSync(
    path.join(__dirname, `../${module_folder}/${file}_run.js`),
    run_code,
    {
      encoding: "utf-8",
    }
  );
  fs.writeFileSync(
    path.join(__dirname, `../${module_folder}/${file}.js`),
    final_code,
    {
      encoding: "utf-8",
    }
  );
});

//module: require_code
var require_code_action = generate_files
  .map(function (generate_file) {
    return "var " + generate_file + " = require('./" + generate_file + "')";
  })
  .join("\n");

//module: 生成main
fs.writeFileSync(
  path.join(__dirname, `../${module_folder}/main.js`),
  require_code +
    "\n" +
    require_code_action +
    "\n" +
    `
  function main() {
    var index = 0;
    while (true) {
      if (index > 100) {
        return;
      }
      video.go_page_from_home();
      sleep(2 * 1000);
      treasure.go_page_from_home();
      sleep(2 * 1000);
      ad.go_page_from_home();
      sleep(2 * 1000);
      console.show();
      console.log("在等待下一个轮回,5s");
      sleep(5 * 1000);
      console.hide();
      sleep(2 * 1000);
      index = index + 1;
    }
  }
  main();
  `,
  {
    encoding: "utf-8",
  }
);
//run: 生成main

//run: export_code
var export_code_action = generate_files
  .map(function (generate_file) {
    var code = fs.readFileSync(
      path.join(__dirname, `../${all_folder}/${generate_file}.js`),
      {
        encoding: "utf-8",
      }
    );
    return code.replace(/var[\w\W]+?require[\w\W]+?;/gi, "");
  })
  .join("\n");

fs.writeFileSync(
  path.join(__dirname, `../${run_folder}/main.js`),
  export_code +
    "\n" +
    export_code_action +
    "\n" +
    `function main() {
      var index = 0;
      while (true) {
        if (index > 100) {
          return;
        }
        video.go_page_from_home();
        sleep(2 * 1000);
        treasure.go_page_from_home();
        sleep(2 * 1000);
        ad.go_page_from_home();
        sleep(2 * 1000);
        console.show();
        console.log("在等待下一个轮回,5s");
        sleep(5 * 1000);
        console.hide();
        sleep(2 * 1000);
        index = index + 1;
      }
    }
    main();`,
  {
    encoding: "utf-8",
  }
);
